var util = require('../../utils/util.js');
var api = require('../../config/api.js');
var _Cart= require("../../modules/cart_module.js");
const App = getApp();
Page({
    data: {
        current:1,
        navList: [],
        categoryList: [],
        categoryId:0,
        currentCategory: [],
        scrollLeft: 0,
        scrollTop: 0,
        goodsCount: 0,
        scrollHeight: 0,
        inputValue:'',
        store:'',
        isBottom: false,
        menuButton:{},
        navH:0,
    },
    onLoad: function (options) {
        if(App.globalData.id_for_category!=0){
            this.setData({
                categoryId:App.globalData.id_for_category,
            })
            App.globalData.id_for_category = 0
        }

        this.setData({
            menuButton: App.globalData.menuButton,
            navH: App.globalData.barHeight,
        })
    },
    onShareAppMessage: function (options) {
        console.log({options});
		const { id, img } = options.target.dataset;
		return {
			title: '这个商品真的太划算了，我已经下单了！',
			imageUrl: img, // 图片 URL
			path: "/pages/goods/goods?id=" + id
		}
	},
    onPullDownRefresh: function () {
        this.getCatalog();
    },
    searchInput:function(e){
        this.setData({
            inputValue:e.detail.value
        })

    },
    setSearchStorage:function(){
        console.log(111,this.data.inputValue)
        wx.navigateTo({
            url: '/pages/search/search?search='+this.data.inputValue
        })
    },
    getCatalog: function () {
        //CatalogList
        let that = this;
        wx.showLoading({
            title: '加载中...',
        });
        util.request(api.CatalogList).then(function (res) {
            that.setData({
                navList: res.result,
            });
            wx.hideLoading();
            let categoryId = that.data.categoryId;
            if(categoryId==0){
                categoryId = res.result[0].id
            }
            that.getCurrentCategory(categoryId)
        });

    },
    getCurrentCategory: function (id) {
        let that = this;
        util.request(api.FindCommodityByChangeAttribute+'?current='+this.data.current+'&size='+10, { categoryId: id },'POST')
            .then(function (res) {
                if(res.result.length > 0){
                    that.setData({
                        currentCategory: that.data.currentCategory.concat(res.result),
                        categoryId:id,
                        current:that.data.current+1
                    });
                }else{
                    that.setData({
                        isBottom: true,
                    })
                }
            });
    },
    loadMore: function () {
        if(this.data.isBottom){
            return
        }
        this.getCurrentCategory();
    },
    onReady: function () {
        // 页面渲染完成
    },
    onShow: function () {
        this.getCatalog();
        if(App.globalData.cartNum > 0){
            wx.setTabBarBadge({
                index: 3,
                text: App.globalData.cartNum.toString()
            })
        }else{
            wx.removeTabBarBadge({index:3})
        }
    },
    onHide: function () {
        // 页面隐藏
    },
    onUnload: function () {
        // 页面关闭
    },
    getList: function () {
        var that = this;
    },
    addCart(event){
        // this.triggerEvent("add", this.data.item.commodityId, 1, "add");
        this.cartUpdate(event.currentTarget.dataset.id,1)
    },
    minusCart(event){
        // this.triggerEvent("minus", this.data.item.commodityId, -1, "minus");
        this.cartUpdate(event.currentTarget.dataset.id,-1)

    },
    async cartUpdate(id,update){
        const list = this.data.currentCategory;
        // 请求
        const res = await _Cart._cartUpdate({
            id:id,
            update: update,
            list,
            changeProperty: "hasNumber"
        }).catch(err=>{
            console.log(err)
        });
        // 本地更新购物车数量
        if(res.id){
            const item_str = 'currentCategory[' + res.index + '].hasNumber'
            this.setData({
                [item_str]: res.num
            })
        }
    },
    switchCate: function (event) {
        var that = this;
        var currentTarget = event.currentTarget;
        if (this.data.currentCategory.categoryId == event.currentTarget.dataset.id) {
            return false;
        }
        this.setData({
            current:1,
            currentCategory:[],
            isBottom:false,
        })
        this.getCurrentCategory(event.currentTarget.dataset.id);
    }
})
